package com.ruoyi.safe.controller.web;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.CommonCheckType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.safe.service.ICommonCheckTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 检查类型Controller
 *
 * @author ruoyi
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/common/type")
public class CommonCheckTypeController extends BaseController {
    @Autowired
    private ICommonCheckTypeService commonCheckTypeService;

    /**
     * 查询检查类型列表
     */
    @PreAuthorize("@ss.hasPermi('common:type:list')")
    @GetMapping("/list")
    public AjaxResult list(CommonCheckType commonCheckType) {
        List<CommonCheckType> list = commonCheckTypeService.selectCommonCheckTypeList(commonCheckType);
        return success(list);
    }

    /**
     * 导出检查类型列表
     */
    @PreAuthorize("@ss.hasPermi('common:type:export')")
    @Log(title = "检查类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommonCheckType commonCheckType) {
        List<CommonCheckType> list = commonCheckTypeService.selectCommonCheckTypeList(commonCheckType);
        ExcelUtil<CommonCheckType> util = new ExcelUtil<CommonCheckType>(CommonCheckType.class);
        util.exportExcel(response, list, "检查类型数据");
    }

    /**
     * 获取检查类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('common:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(commonCheckTypeService.selectCommonCheckTypeById(id));
    }

    /**
     * 新增检查类型
     */
    @PreAuthorize("@ss.hasPermi('common:type:add')")
    @Log(title = "检查类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommonCheckType commonCheckType) {
        return toAjax(commonCheckTypeService.insertCommonCheckType(commonCheckType));
    }

    /**
     * 修改检查类型
     */
    @PreAuthorize("@ss.hasPermi('common:type:edit')")
    @Log(title = "检查类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommonCheckType commonCheckType) {
        return toAjax(commonCheckTypeService.updateCommonCheckType(commonCheckType));
    }

    /**
     * 删除检查类型
     */
    @PreAuthorize("@ss.hasPermi('common:type:remove')")
    @Log(title = "检查类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(commonCheckTypeService.deleteCommonCheckTypeByIds(ids));
    }

    /**
     * 检查类型树
     * @param commonCheckType
     * @return
     */
    @PostMapping("/treeselect")
    public AjaxResult treeselect(@RequestBody CommonCheckType commonCheckType) {
        commonCheckType.setStatus("0");
        List<CommonCheckType> commonCheckTypes = commonCheckTypeService.selectCommonCheckTypeList(commonCheckType);
        List<TreeSelect> treeSelects = commonCheckTypeService.buildCheckTypeTreeSelect(commonCheckTypes);
        return AjaxResult.success(treeSelects);
    }
    /**
     * 根据父级id查询检查类型
     */
    @GetMapping("/gettype")
    public AjaxResult selectListTypeByFive() {
        return success(commonCheckTypeService.selectCommonCheckTypeListByParentId());
    }
}
